package com.loe.test.chart

import android.graphics.Color
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.ValueFormatter

class LineChartManager(var chart: LineChart)
{
    private val textColor = Color.parseColor("#888888")
    private val mainColor = Color.parseColor("#226AFF")
    private val fillColor = Color.parseColor("#01226AFF")

    init
    {
        initChart()
    }

    //初始化
    private fun initChart()
    {
        //图相对于上下左右的偏移
        chart.setExtraOffsets(15f, 35f, 30f, 25f)
        chart.isScaleXEnabled = true
        chart.isScaleYEnabled = false
        /*** X轴  */
        val xAxis = chart.xAxis
        //将X轴的值显示在中央
//        xAxis.setCenterAxisLabels(true);
        xAxis.setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线）
        xAxis.textColor = textColor
        xAxis.textSize = 12f
        xAxis.yOffset = 10f
        xAxis.axisLineColor = Color.TRANSPARENT //X轴颜色
        xAxis.axisLineWidth = 0f //X轴粗细
        xAxis.position = XAxis.XAxisPosition.BOTTOM //X轴所在位置   默认为上面
        xAxis.granularity = 1f // 最小单位
        xAxis.setLabelCount(17, false)
        /*** Y轴  */
        val axisLeft = chart.axisLeft
        axisLeft.setDrawGridLines(true) //是否绘制Y轴上的网格线（背景里面的横线）
        axisLeft.gridColor = -0x222223
        axisLeft.textColor = textColor
        axisLeft.textSize = 12f
        axisLeft.axisLineColor = Color.TRANSPARENT //Y轴颜色
        axisLeft.axisLineWidth = 0f //Y轴粗细
        chart.axisRight.isEnabled = false //隐藏右侧Y轴   默认是左右两侧都有Y轴

//        AxisLeft.setAxisMaximum(15);   //Y轴最大数值
        axisLeft.axisMinimum = -1f //Y轴最小数值
        //Y轴坐标的个数    第二个参数一般填false     true表示强制设置标签数 可能会导致X轴坐标显示不全等问题
        axisLeft.setLabelCount(5, false)

        chart.marker = LineMarkerView(chart.context)

        //取消右下角描述
        chart.description.isEnabled = false
        //图标的背景色
//        barChart.setBackgroundColor(Color.WHITE);
//        设置barChart图表转动阻力摩擦系数[0,1]



        //获取图例
        val legend = chart.legend
        legend.isEnabled = false // 是否显示
        legend.orientation = Legend.LegendOrientation.HORIZONTAL //设置图例水平显示
        legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP //顶部
        legend.horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //右对其
        legend.formSize = 12f
        legend.xEntrySpace = 20f //x轴的间距
        legend.yEntrySpace = 0f //y轴的间距
        legend.yOffset = 28f //图例的y偏移量
        legend.xOffset = 10f //图例x的偏移量
        legend.textColor = textColor //图例文字的颜色
        legend.textSize = 18f //图例文字的大小
    }

    /**
     * 显示
     */
    fun show(list: List<Pair<String?, Int?>>)
    {
        val listEntries: MutableList<Entry> = ArrayList()
        for (i in list.indices)
        {
            listEntries.add(Entry(i.toFloat(), list[i].second!!.toFloat()))
        }

//        chart.getXAxis().setLabelCount(list.size(), false);
        chart.xAxis.valueFormatter = object : ValueFormatter()
        {
            override fun getAxisLabel(v: Float, axis: AxisBase): String
            {
                val i = v.toInt()
                return if (i >= 0 && i < list.size)
                {
                    list[v.toInt()].first!!
                } else ""
            }
        }
        if(list.size > 17)
        {
            chart.zoom(list.size / 17f, 1f, 0f, 0f)
        }

        val dataSet = LineDataSet(listEntries, "")
        val data = LineData(dataSet)
//        data.setBarWidth(0.65f);
        dataSet.highLightColor = mainColor
        dataSet.highlightLineWidth = 1f
//        dataSet.isHighlightEnabled = true
        dataSet.setDrawHorizontalHighlightIndicator(false)
        dataSet.setColors(mainColor) //设置线段多种颜色  循环使用
        dataSet.valueTextColor = Color.TRANSPARENT
        dataSet.setDrawCircles(false)
        dataSet.setDrawFilled(true)
        dataSet.fillColor = mainColor
        dataSet.fillAlpha = 20
        dataSet.valueFormatter = object : ValueFormatter()
        {
            override fun getFormattedValue(value: Float): String
            {
                return value.toInt().toString() + "人"
            }
        }
        dataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
        chart.data = data
    }
}